/**
 * 游戏的主文件
 */
package wolfKill;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;


/**
 * @author blank
 *
 */
public class Game {
	// 创建玩家列表
	List<Player> list = new ArrayList<Player>();
	// 获取控制台输入信息的对象
	Scanner scanner = new Scanner(System.in);
	// 创建死亡名单
	List<Player> diePlayer = new ArrayList<Player>();
	// 判断女巫的状态
	boolean witchDie = false;
	// 解药
	int antidote = 1;
	// 毒药
	int poison = 1;
	//最终结果
	int result=-1;

	public static void main(String[] args) {
		Game game = new Game();
		game.init();
		
		  while (true) { 
		//黑夜阶段 
		  game.dark();
		// 天亮了
		game.day();
		game.result = game.winner();
		if (game.result == 2) {
			System.out.println("好人获胜");
			break;
		}
		if (game.result == 1) {
			System.out.println("狼人获胜！");
			break;
		}
		  }
		 
		
	}

	/**获得游戏结果
	 * @return
	 */
	private int winner() {
		// TODO Auto-generated method stub
		//狼数
		int wolfs = 0;
		//民数
		int peoples = 0;
	//神数
		int gods = 0; 
		for (Player player : list) {
			if (player.state != 0) {
				switch (player.camp) {
				case "狼":
					wolfs++;
					break;
				case "民":
					peoples++;
					break;
				case "神":
					gods++;
					break;
				}
			}
		}
		//当有一个阵营的死亡人数达至4人，这一方阵营全灭
		if (wolfs == 4) {
			return 1;
		} else if (peoples == 4) {
			return 2;
		} else if (gods == 4) {
			return 2;
		}
		
		return 0;
	}

	/**
	 * 
	 */
	private void day() {
		// TODO Auto-generated method stub
		System.out.println("黑夜结束，天亮了");
		if (diePlayer == null && diePlayer.isEmpty()) {
			System.out.println("昨晚是平安夜");
		} else {
			for (Player player : diePlayer) {
				System.out.println("昨天晚上" + player.getName() + "死了");
			}

		}
		speak();
		vote();
	}

	/**
	 * 白天阶段——投票
	 */
	private void vote() {
		// TODO Auto-generated method stub
		int voteChoose=-1;
		System.out.println("现在是投票放逐阶段");
		System.out.println("请选择一名玩家，将他放逐");
		for (Player player : list) {
			if (player.getState() == 0) {
				System.out.print(player.getName());
			}
		}
		System.out.println("选择：");
		voteChoose=Integer.parseInt(scanner.nextLine());
		if (list.get(voteChoose-1).getIdentity().equals("白痴")) {
			System.out.println("免疫放逐");
		}
		if (list.get(voteChoose-1).getIdentity().equals("猎人")) {
			list.get(voteChoose-1).state=0;
			hunter();
		}
		
	}

	/**
	 * 猎人方法
	 */
	private void hunter() {
		// TODO Auto-generated method stub
		//猎人时间
		int hunterChooese=-1;
		System.out.println("现在是猎人时刻");
		System.out.println("请选择你要带走的人");
		for (Player player : list) {
			if (player.state == 0) {
				System.out.print(player.name + " ");
			}
		}
		hunterChooese=Integer.parseInt(scanner.nextLine());
		list.get(hunterChooese-1).state=0;
		System.out.println("猎人带走了"+list.get(hunterChooese-1).getName());
	}

	/**
	 * 玩家发言
	 */
	private void speak() {
		// TODO Auto-generated method stub
		// 死掉的玩家发表遗言
		if (diePlayer != null && !diePlayer.isEmpty()) {
			System.out.println("开始发表遗言");
			for (Player player : diePlayer) {
				System.out.print(player.getName() + ":");
				scanner.nextLine();
			}
		}
		// 活下来的人随机发言
		int randomNum = (int) (Math.random() * list.size());
		System.out.println("发言阶段");
		for (int i = randomNum; i < list.size();) {
			if (list.get(i).state == 0) {
				System.out.println(list.get(i).name + "发言：");
				scanner.nextLine();
			}
			i++;
			// 当玩家达到最大值，从玩家1开始发言
			if (i == list.size()) {
				i = 0;
			}
			// 如果已经经过了一个轮回，则退出
			if (i == randomNum) {
				break;
			}
		}

	}

	// 初始化游戏运行信息
	public void init() {
		String identify[] = { "预言家", "女巫", "猎人", "白痴", "平民", "平民", "平民", "平民", "狼人", "狼人", "狼人", "狼人" };
		List<Integer> nums = new ArrayList<>();
		for (int i = 0; i < identify.length; i++) {
			nums.add(i);
		}
		Collections.shuffle(nums);
		// 创建玩家对象
		Player player = null;
		for (int i = 0; i < identify.length; i++) {
			player = new Player();
			player.setName("玩家" + (i + 1));
			player.setIdentity(identify[nums.get(i)]);
			player.setState(0);
			list.add(player);
		}
		// 展示玩家信息
		System.out.println("玩家身份如下：");
		for (int i = 0; i < identify.length; i++) {
			System.out.print(list.get(i).toString());
			System.out.println("     " + list.get(i + 1).toString());
			i++;
		}

		System.out.println("-------------游戏开始------------");
		System.out.println("---------------------------------");

	}

	public void dark() {
		System.out.println("天黑了");
		// 清空死亡玩家列表
		diePlayer.clear();
		System.out.println("狼人开始行动，请选择一名猎杀");
		for (Player player : list) {
			if (player.state == 0) {
				System.out.print(player.getName() + " ");
			}
		}
		System.out.println();
		System.out.print("目标： ");
		int wolfChoose = Integer.parseInt(scanner.nextLine());
		list.get(wolfChoose - 1).setState(1);
		// 今晚被杀的人
		int killTonight = -1;
		killTonight = wolfChoose - 1;
		diePlayer.add(list.get(killTonight));
		System.out.println("狼人行动结束");
		rophet(killTonight);
		// 判断今晚被杀的是不是女巫
		if (diePlayer.get(0).getIdentity().equals("女巫")) {
			witchDie = true;
		}
		// 判断女巫是否死亡，如果死了，女巫就没有任何操作了
		if (witchDie) {
			// 没有任何操作
		} else {
			witch(killTonight);
		}
	}

	/**
	 * @param killTonight
	 */
	private void witch(int killTonight) {
		// TODO Auto-generated method stub
		System.out.println("女巫开始行动");
		int witchChoose;
		// 有解药存在时
		if (antidote == 1) {
			if (killTonight != -1) {
				System.out.println("今天晚上" + list.get(killTonight).getName() + "死了");
				System.out.println("是否解他？1：救  2：不救");
				witchChoose = Integer.parseInt(scanner.nextLine());
				if (witchChoose == 1) {
					// 将解药用掉了
					antidote = 0;
					// 复活今晚被狼人杀掉的玩家
					list.get(killTonight).state = 0;
					diePlayer.clear();
					System.out.println("女巫行动结束-");
					return;
				}
			}
		}
		// 是否使用毒药
		if (poison == 1) { 
			System.out.println("请选择你要毒死的人");
			for (Player player : list) {
				if (player.state == 0) {
					System.out.print(player.name + " ");
				}
			}
			System.out.println();
			witchChoose = Integer.parseInt(scanner.nextLine());
			list.get(witchChoose - 1).state = 2;
			diePlayer.add(list.get(witchChoose - 1));
			poison = 0;
		}
		System.out.println("女巫行动结束-");

	}

	public void rophet(int killTonight) {
		System.out.println("预言家开始行动");
		System.out.println("请选择查验一名玩家的身份");
		// 预言家选择的人
		int rophetChoose = Integer.parseInt(scanner.nextLine());
		for (Player player : list) {
			// 预言家的判断逻辑是：检测循环的玩家是不是存和的预言家或者 当晚被狼人杀的预言家，那么他可以最后操作一下
			if ((player.identity.equals("预言家") && player.getState() == 0)
					|| (list.get(killTonight).getIdentity().equals("预言家") && player.getState() == 1)) {
				if (list.get(rophetChoose - 1).camp.equals("神") || list.get(rophetChoose - 1).camp.equals("民"))
					System.out.println("该名玩家身份：" + "好人");
				else
					System.out.println("该名玩家身份：" + "狼人");
				break;
			}
		}
		System.out.println("预言家行动结束");
	}
}
